package com.jdy.haoduoai.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.jdy.haoduoai.adapter.ImageBucketAdapter;
import com.jdy.haoduoai.model.ImageBucket;
import com.jdy.haoduoai.util.CustomConstants;
import com.jdy.haoduoai.util.ImageFetcher;
import com.jdy.haoduoai.util.IntentConstants;
import com.ycsj.goldmedalnewconcept.R;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

//import com.jdy.haoduoai.R;

/**
 * 选择相册
 * 
 */

public class ImageBucketChooseActivity extends BaseActivity {
  private ImageFetcher mHelper;
  private List<ImageBucket> mDataList = new ArrayList<ImageBucket>();
  private ListView mListView;
  private ImageBucketAdapter mAdapter;
  private int availableSize;
  private boolean FromAddHabitClockActivity = false;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_image_bucket_choose);

    setTitle("相册");
    mHelper = ImageFetcher.getInstance(getApplicationContext());
    initData();
    initView();
  }

  private void initData() {
    mDataList = mHelper.getImagesBucketList(true);
    availableSize = getIntent().getIntExtra(
        IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE,
        CustomConstants.MAX_IMAGE_SIZE);

    Bundle bundle = getIntent().getExtras();

    if (bundle != null && bundle.getString("classFrom") != null && bundle.getString("classFrom").equals(AddHabitClockActivity.class.toString())) {
      FromAddHabitClockActivity = true;
    }
  }

  private void initView() {
    mListView = (ListView) findViewById(R.id.listview);
    mAdapter = new ImageBucketAdapter(this, mDataList);
    mListView.setAdapter(mAdapter);
    TextView titleTv = (TextView) findViewById(R.id.title);
    titleTv.setText("相册");
    mListView.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
          long id) {

        selectOne(position);

        Intent intent = new Intent(ImageBucketChooseActivity.this,
            ImageChooseActivity.class);
        intent.putExtra(IntentConstants.EXTRA_IMAGE_LIST,
            (Serializable) mDataList.get(position).imageList);
        intent.putExtra(IntentConstants.EXTRA_BUCKET_NAME,
            mDataList.get(position).bucketName);
        intent
            .putExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE, availableSize);
        intent.putExtra("FromAddHabitClockActivity",FromAddHabitClockActivity);

        startActivity(intent);
      }
    });

    // TextView cancelTv = (TextView) findViewById(R.id.action);
    // cancelTv.setOnClickListener(new OnClickListener() {
    //
    // @Override
    // public void onClick(View v) {
    // // Intent intent = new Intent(ImageBucketChooseActivity.this,
    // // PublishActivity.class);
    // // startActivity(intent);
    // }
    // });
  }

  private void selectOne(int position) {
    int size = mDataList.size();
    for (int i = 0; i != size; i++) {
      if (i == position)
        mDataList.get(i).selected = true;
      else {
        mDataList.get(i).selected = false;
      }
    }
    mAdapter.notifyDataSetChanged();
  }

}
